load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc:cc_test.bzl", "cc_test")

cc_library(
    name = "c-blosc2",
    srcs = [
        "blosc/b2nd.c",
        "blosc/b2nd-private.h",
        "blosc/b2nd_utils.c",
        "blosc/bitshuffle-avx2.c",
        "blosc/bitshuffle-avx2.h",
        "blosc/bitshuffle-generic.c",
        "blosc/bitshuffle-generic.h",
        "blosc/bitshuffle-sse2.c",
        "blosc/bitshuffle-sse2.h",
        "blosc/blosc-private.h",
        "blosc/blosc2.c",
        "blosc/blosc2-stdio.c",
        "blosc/blosclz.c",
        "blosc/blosclz.h",
        "blosc/delta.c",
        "blosc/directories.c",
        "blosc/fastcopy.c",
        "blosc/frame.c",
        "blosc/frame.h",
        "blosc/schunk.c",
        "blosc/schunk-private.h",
        "blosc/sframe.c",
        "blosc/sframe.h",
        "blosc/shuffle.c",
        "blosc/shuffle.h",
        "blosc/shuffle-avx2.c",
        "blosc/shuffle-avx2.h",
        "blosc/shuffle-generic.c",
        "blosc/shuffle-generic.h",
        "blosc/shuffle-sse2.c",
        "blosc/shuffle-sse2.h",
        "blosc/stune.c",
        "blosc/threading.h",
        "blosc/timestamp.c",
        "blosc/trunc-prec.c",
        "plugins/codecs/codecs-registry.c",
        "plugins/codecs/ndlz/ndlz.c",
        "plugins/codecs/ndlz/ndlz.h",
        "plugins/codecs/ndlz/ndlz-private.h",
        "plugins/codecs/ndlz/ndlz4x4.c",
        "plugins/codecs/ndlz/ndlz4x4.h",
        "plugins/codecs/ndlz/ndlz8x8.c",
        "plugins/codecs/ndlz/ndlz8x8.h",
        "plugins/codecs/ndlz/xxhash.c",
        "plugins/codecs/ndlz/xxhash.h",
        "plugins/codecs/zfp/blosc2-zfp.c",
        "plugins/codecs/zfp/src/bitstream.c",
        "plugins/codecs/zfp/src/block1.h",
        "plugins/codecs/zfp/src/block2.h",
        "plugins/codecs/zfp/src/block3.h",
        "plugins/codecs/zfp/src/block4.h",
        "plugins/codecs/zfp/src/decode1d.c",
        "plugins/codecs/zfp/src/decode1f.c",
        "plugins/codecs/zfp/src/decode1i.c",
        "plugins/codecs/zfp/src/decode1l.c",
        "plugins/codecs/zfp/src/decode2d.c",
        "plugins/codecs/zfp/src/decode2f.c",
        "plugins/codecs/zfp/src/decode2i.c",
        "plugins/codecs/zfp/src/decode2l.c",
        "plugins/codecs/zfp/src/decode3d.c",
        "plugins/codecs/zfp/src/decode3f.c",
        "plugins/codecs/zfp/src/decode3i.c",
        "plugins/codecs/zfp/src/decode3l.c",
        "plugins/codecs/zfp/src/decode4d.c",
        "plugins/codecs/zfp/src/decode4f.c",
        "plugins/codecs/zfp/src/decode4i.c",
        "plugins/codecs/zfp/src/decode4l.c",
        "plugins/codecs/zfp/src/encode1d.c",
        "plugins/codecs/zfp/src/encode1f.c",
        "plugins/codecs/zfp/src/encode1i.c",
        "plugins/codecs/zfp/src/encode1l.c",
        "plugins/codecs/zfp/src/encode2d.c",
        "plugins/codecs/zfp/src/encode2f.c",
        "plugins/codecs/zfp/src/encode2i.c",
        "plugins/codecs/zfp/src/encode2l.c",
        "plugins/codecs/zfp/src/encode3d.c",
        "plugins/codecs/zfp/src/encode3f.c",
        "plugins/codecs/zfp/src/encode3i.c",
        "plugins/codecs/zfp/src/encode3l.c",
        "plugins/codecs/zfp/src/encode4d.c",
        "plugins/codecs/zfp/src/encode4f.c",
        "plugins/codecs/zfp/src/encode4i.c",
        "plugins/codecs/zfp/src/encode4l.c",
        "plugins/codecs/zfp/src/inline/inline.h",
        "plugins/codecs/zfp/src/template/template.h",
        "plugins/codecs/zfp/src/traitsd.h",
        "plugins/codecs/zfp/src/traitsf.h",
        "plugins/codecs/zfp/src/traitsi.h",
        "plugins/codecs/zfp/src/traitsl.h",
        "plugins/codecs/zfp/src/zfp.c",
        "plugins/codecs/zfp/zfp-private.h",
        "plugins/filters/bytedelta/bytedelta.c",
        "plugins/filters/bytedelta/bytedelta.h",
        "plugins/filters/filters-registry.c",
        "plugins/filters/int_trunc/int_trunc.c",
        "plugins/filters/int_trunc/int_trunc.h",
        "plugins/filters/ndcell/ndcell.c",
        "plugins/filters/ndcell/ndcell.h",
        "plugins/filters/ndmean/ndmean.c",
        "plugins/filters/ndmean/ndmean.h",
        "plugins/tuners/tuners-registry.c",
    ] + select({
        "@platforms//os:windows": glob([
            "blosc/win32/threading.c",
        ]),
        "//conditions:default": [],
    }),
    hdrs = [
        "blosc/context.h",
        "blosc/delta.h",
        "blosc/fastcopy.h",
        "blosc/stune.h",
        "blosc/trunc-prec.h",
        "plugins/codecs/zfp/blosc2-zfp.h",
    ] + glob([
        "plugins/codecs/zfp/include/**/*.h",
        "include/**/*.h",
    ]),
    copts = select({
        "@platforms//os:linux": [
            "-msse4.1",
        ],
        "//conditions:default": [
        ],
    }),
    defines = [
        "HAVE_PLUGINS",
        "HAVE_ZSTD",
        #"HAVE_ZLIB_NG",
    ],
    includes = [
        "blosc",
        "include",
        "plugins/codecs",
        "plugins/codecs/ndlz",
        "plugins/codecs/zfp/include",
        "plugins/codecs/zfp/src",
    ],
    textual_hdrs = [
        "plugins/codecs/zfp/src/inline/bitstream.c",
        "plugins/codecs/zfp/src/share/omp.c",
        "plugins/codecs/zfp/src/share/parallel.c",
        "plugins/codecs/zfp/src/template/codec.c",
        "plugins/codecs/zfp/src/template/codec.h",
        "plugins/codecs/zfp/src/template/codec1.c",
        "plugins/codecs/zfp/src/template/codec2.c",
        "plugins/codecs/zfp/src/template/codec3.c",
        "plugins/codecs/zfp/src/template/codec4.c",
        "plugins/codecs/zfp/src/template/codecf.c",
        "plugins/codecs/zfp/src/template/compress.c",
        "plugins/codecs/zfp/src/template/decode.c",
        "plugins/codecs/zfp/src/template/decode1.c",
        "plugins/codecs/zfp/src/template/decode2.c",
        "plugins/codecs/zfp/src/template/decode3.c",
        "plugins/codecs/zfp/src/template/decode4.c",
        "plugins/codecs/zfp/src/template/decodef.c",
        "plugins/codecs/zfp/src/template/decodei.c",
        "plugins/codecs/zfp/src/template/decompress.c",
        "plugins/codecs/zfp/src/template/encode.c",
        "plugins/codecs/zfp/src/template/encode2.c",
        "plugins/codecs/zfp/src/template/encode3.c",
        "plugins/codecs/zfp/src/template/encode4.c",
        "plugins/codecs/zfp/src/template/encode1.c",
        "plugins/codecs/zfp/src/template/encodef.c",
        "plugins/codecs/zfp/src/template/encodei.c",
        "plugins/codecs/zfp/src/template/revencode.c",
        "plugins/codecs/zfp/src/template/revencode2.c",
        "plugins/codecs/zfp/src/template/revencode3.c",
        "plugins/codecs/zfp/src/template/revencode4.c",
        "plugins/codecs/zfp/src/template/revencode1.c",
        "plugins/codecs/zfp/src/template/revencodef.c",
        "plugins/codecs/zfp/src/template/ompcompress.c",
        "plugins/codecs/zfp/src/template/revcodecf.c",
        "plugins/codecs/zfp/src/template/revdecode.c",
        "plugins/codecs/zfp/src/template/revdecode1.c",
        "plugins/codecs/zfp/src/template/revdecode2.c",
        "plugins/codecs/zfp/src/template/revdecode3.c",
        "plugins/codecs/zfp/src/template/revdecode4.c",
        "plugins/codecs/zfp/src/template/revdecodef.c",
    ],
    visibility = ["//visibility:public"],
    deps = [
        "@lz4//:lz4_hc",
        "@zlib-ng//:zlib-ng",
        "@zstd",
    ],
)

cc_test(
    name = "test_ndlz",
    srcs = [
        "plugins/codecs/ndlz/test_ndlz.c",
        "tests/test_common.h",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_b2nd_append",
    srcs = [
        "tests/b2nd/cutest.h",
        "tests/b2nd/test_b2nd_append.c",
        "tests/b2nd/test_common.h",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_b2nd_copy_buffer",
    srcs = [
        "tests/b2nd/cutest.h",
        "tests/b2nd/test_b2nd_copy_buffer.c",
        "tests/b2nd/test_common.h",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_b2nd_copy",
    srcs = [
        "tests/b2nd/cutest.h",
        "tests/b2nd/test_b2nd_copy.c",
        "tests/b2nd/test_common.h",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_b2nd_delete",
    srcs = [
        "tests/b2nd/cutest.h",
        "tests/b2nd/test_b2nd_delete.c",
        "tests/b2nd/test_common.h",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_b2nd_full",
    srcs = [
        "tests/b2nd/cutest.h",
        "tests/b2nd/test_b2nd_full.c",
        "tests/b2nd/test_common.h",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_b2nd_get_slice",
    srcs = [
        "tests/b2nd/cutest.h",
        "tests/b2nd/test_b2nd_get_slice.c",
        "tests/b2nd/test_common.h",
    ],
    deps = [":c-blosc2"],
)

# Does not work on Windows
#cc_test(
#    name = "test_b2nd_get_slice_buffer",
#    srcs = [
#       "tests/b2nd/cutest.h",
#        "tests/b2nd/test_b2nd_get_slice_buffer.c",
#        "tests/b2nd/test_common.h",
#    ],
#    deps = [":c-blosc2"],
#)

cc_test(
    name = "test_b2nd_insert",
    srcs = [
        "tests/b2nd/cutest.h",
        "tests/b2nd/test_b2nd_insert.c",
        "tests/b2nd/test_common.h",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_b2nd_metalayers",
    srcs = [
        "tests/b2nd/cutest.h",
        "tests/b2nd/test_b2nd_metalayers.c",
        "tests/b2nd/test_common.h",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_b2nd_open_offset",
    srcs = [
        "tests/b2nd/cutest.h",
        "tests/b2nd/test_b2nd_open_offset.c",
        "tests/b2nd/test_common.h",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_b2nd_persistency",
    srcs = [
        "tests/b2nd/cutest.h",
        "tests/b2nd/test_b2nd_persistency.c",
        "tests/b2nd/test_common.h",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_b2nd_resize",
    srcs = [
        "tests/b2nd/cutest.h",
        "tests/b2nd/test_b2nd_resize.c",
        "tests/b2nd/test_common.h",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_b2nd_roundtrip",
    srcs = [
        "tests/b2nd/cutest.h",
        "tests/b2nd/test_b2nd_roundtrip.c",
        "tests/b2nd/test_common.h",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_b2nd_save",
    srcs = [
        "tests/b2nd/cutest.h",
        "tests/b2nd/test_b2nd_save.c",
        "tests/b2nd/test_common.h",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_b2nd_serialize",
    srcs = [
        "tests/b2nd/cutest.h",
        "tests/b2nd/test_b2nd_serialize.c",
        "tests/b2nd/test_common.h",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_bitshuffle_leftovers",
    srcs = [
        "tests/test_bitshuffle_leftovers.c",
        "tests/test_common.h",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_delete_chunk",
    srcs = [
        "tests/test_common.h",
        "tests/test_delete_chunk.c",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_delta_schunk",
    srcs = [
        "tests/test_common.h",
        "tests/test_delta_schunk.c",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_delta",
    srcs = [
        "tests/test_common.h",
        "tests/test_delta.c",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_noinit",
    srcs = [
        "tests/test_common.h",
        "tests/test_noinit.c",
    ],
    target_compatible_with = select({
        "@platforms//os:windows": ["@platforms//:incompatible"],
        "//conditions:default": [],
    }),
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_nolock",
    srcs = [
        "tests/test_common.h",
        "tests/test_nolock.c",
    ],
    target_compatible_with = select({
        "@platforms//os:windows": ["@platforms//:incompatible"],
        "//conditions:default": [],
    }),
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_nthreads",
    srcs = [
        "tests/test_common.h",
        "tests/test_nthreads.c",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_postfilter",
    srcs = [
        "tests/test_common.h",
        "tests/test_postfilter.c",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_prefilter",
    srcs = [
        "tests/test_common.h",
        "tests/test_prefilter.c",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_reorder_offsets",
    srcs = [
        "tests/test_common.h",
        "tests/test_reorder_offsets.c",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_schunk_frame",
    srcs = [
        "tests/test_common.h",
        "tests/test_schunk_frame.c",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_schunk_header",
    srcs = [
        "tests/test_common.h",
        "tests/test_schunk_header.c",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_schunk",
    srcs = [
        "tests/test_common.h",
        "tests/test_schunk.c",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_set_slice_buffer",
    srcs = [
        "tests/test_common.h",
        "tests/test_set_slice_buffer.c",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_sframe_lazychunk",
    srcs = [
        "tests/test_common.h",
        "tests/test_sframe_lazychunk.c",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_sframe",
    srcs = [
        "tests/test_common.h",
        "tests/test_sframe.c",
    ],
    deps = [":c-blosc2"],
)

#cc_test(
#    name = "test_shuffle_roundtrip_sse2",
#    srcs = [
#        "tests/cutest.h",
#        "tests/test_common.h",
#        "tests/test_shuffle_roundtrip_sse2.c",
#    ],
#    data = ["tests/test_shuffle_roundtrip_sse2.csv"],
#    deps = [":c-blosc2"],
#)

cc_test(
    name = "test_small_chunks",
    srcs = [
        "tests/cutest.h",
        "tests/test_common.h",
        "tests/test_small_chunks.c",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_udio",
    srcs = [
        "tests/cutest.h",
        "tests/test_common.h",
        "tests/test_udio.c",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_update_chunk",
    srcs = [
        "tests/test_common.h",
        "tests/test_update_chunk.c",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_urcodecs",
    srcs = [
        "tests/cutest.h",
        "tests/test_urcodecs.c",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_urfilters",
    srcs = [
        "tests/cutest.h",
        "tests/test_urfilters.c",
    ],
    deps = [":c-blosc2"],
)

cc_test(
    name = "test_zero_runlen",
    srcs = [
        "tests/cutest.h",
        "tests/test_zero_runlen.c",
    ],
    deps = [":c-blosc2"],
)
